package cn.edu.scau.cmi.chensixiao.dao;

import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;

import cn.edu.scau.cmi.chensixiao.domain.Student;
import cn.edu.scau.cmi.chensixiao.dao.BaseHibernateDAO;


public class StudentDAO extends BaseHibernateDAO {
 
  public static final String NAME = "name";

  public void save(Student student) {
	  Session session = getSession();
	  Transaction transaction = session.beginTransaction();
	  
	  session.save(student);
	  
	  transaction.commit();     
	  session.close();
  }

  public void delete(Student student) {
	  Session session = getSession();
	  Transaction transaction = session.beginTransaction();
	  
      session.delete(student);
      
      transaction.commit();
      session.close();
     
  }
  
  public void update(Student student) {
	  Session session = getSession();
	  Transaction transaction = session.beginTransaction();
	  
      session.update(student);
      
      transaction.commit();
      session.close();
  }

  public Student findById(java.lang.Long id) {
	  Session session = getSession();
      Student student = (Student) session.get("cn.edu.scau.cmi.chensixiao.domain.Student", id);
      session.close();
      return student;
    
  }


  public List<Student> findByName(String name) {
	Session session = getSession();
	String queryString = "from Student s where name like '%" + name + "%' ";
	
//    List<Student> students = session.createSQLQuery(queryString).addEntity(Student.class).list();
    
    List<Student> students = session.createQuery(queryString).list();
    
    session.close();
    return students;

  }

  public List<Student> findAll() {
	  Session session = getSession();
      String queryString = "from Student";
//      List<Student> students = session.createSQLQuery(queryString).addEntity(Student.class).list();
      
      List<Student> students = session.createQuery(queryString).list();
      
      session.close();
      return students;

  }

  

}
